//
//  ViewController.m
//  JCUIGradientsDemo
//
//  Created by TR-L on 2019/11/19.
//  Copyright © 2019 Chuan. All rights reserved.
//

#import "ViewController.h"
#import "JCUIGradients.h"

@interface ViewController ()<UITableViewDelegate , UITableViewDataSource>
@property (nonatomic, copy) NSArray *gradients;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gradients = @[@"Warm Flame",@"Night Fade",@"Spring Warmth",@"Juicy Peach",@"Young Passion",@"Lady Lips",@"Sunny Morning",@"Rainy Ashville",@"Frozen Dreams",@"Winter Neva",@"Dusty Grass",@"Tempting Azure",@"Heavy Rain",@"Amy Crisp",@"Mean Fruit",@"Soft Blue",@"Ripe Malinka",@"Cloudy Knoxville",@"Malibu Beach",@"NewLife",@"True Sunset",@"Morpheus Den",@"Rare Wind",@"Near Moon",@"Wild Apple",@"Saint Petersburg",@"Arielles Smile",@"Plum Plate",@"Everlasting Sky",@"Happy Fisher",@"Blessing",@"Sharpeye Eagle",@"Ladoga Bottom",@"Lemon Gate",@"Itmeo Branding",@"Zeus Miracle",@"Old Hat",@"Star Wine",@"Deep Blue",@"Coup De Grace",@"Happy Acid",@"Awesome Pine",@"New York",@"Shy Rainbow",@"Loon Crest",@"Mixed Hopes",@"Fly High",@"Strong Bliss",@"Fresh Milk",@"Snow Again",@"February Ink",@"Kind Steel",@"Soft Grass",@"Grown Early",@"Sharp Blues",@"Shady Water",@"Dirty Beauty",@"Great Whale",@"Teen Notebook",@"Polite Rumors",@"Sweet Period",@"Wide Matrix",@"Soft Cherish",@"Red Salvation",@"Burning Spring",@"Night Party",@"Sky Glider",@"Heaven Peach",@"Purple Division",@"Aqua Splash",@"Above Clouds",@"Spiky Naga",@"Love Kiss",@"Sharp Glass",@"Clean Mirror",@"Premium Dark",@"Cold Evening",@"Cochiti Lake",@"Summer Games",@"Passionate Bed",@"Mountain Rock",@"Desert Hump",@"Jungle Day",@"Phoenix Start",@"October Silence",@"Faraway River",@"Alchemist Lab",@"Over Sun",@"Premium White",@"Mars Party",@"Eternal Constance",@"Japan Blush",@"Smiling Rain",@"Cloudy Apple",@"Big Mango",@"Healthy Water",@"Amour Amour",@"Risky Concrete",@"Strong Stick",@"Vicious Stance",@"Palo Alto",@"Happy Memories",@"Midnight Bloom",@"Crystalline",@"Raccoon Back",@"Party Bliss",@"Confident Cloud",@"Le Cocktail",@"River City",@"Frozen Berry",@"Elegance",@"Child Care",@"Flying Lemon",@"New Retrowave",@"Hidden Jaguar",@"Above The Sky",@"Nega",@"Dense Water",@"Chemic Aqua",@"Seashore",@"Marble Wall",@"Cheerful Caramel",@"Night Sky",@"Magic Lake",@"Young Grass",@"Colorful Peach",@"Gentle Care",@"Plum Bath",@"Happy Unicorn",@"Full Metal",@"African Field",@"Solid Stone",@"Orange Juice",@"Glass Water",@"Slick Carbon",@"North Miracle",@"Fruit Blend",@"Millennium Pine",@"High Flight",@"Mole Hall",@"Earl Gray",@"Space Shift",@"Forest Inei",@"Royal Garden",@"Rich Metal",@"Juicy Cake",@"Smart Indigo",@"Sand Strike",@"Norse Beauty",@"Aqua Guidance",@"Sun Veggie",@"Sea Lord",@"Black Sea",@"Grass Shampoo",@"Landing Aircraft",@"Witch Dance",@"Sleepless Night",@"Angel Care",@"Crystal River",@"Soft Lipstick",@"Salt Mountain",@"Perfect White",@"Fresh Oasis",@"Strict November",@"Morning Salad",@"Deep Relief",@"Sea Strike",@"Night Call",@"Supreme Sky",@"Light Blue",@"Mind Crawl",@"Lily Meadow",@"Sugar Lollipop",@"Sweet Dessert",@"Magic Ray",@"Teen Party",@"Frozen Heat",@"Gagarin View",@"Fabled Sunset",@"Perfect Blue"];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.gradients.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ViewControllerCell" forIndexPath:indexPath];
    cell.index = indexPath.row + 1;
    cell.textLabel.text = self.gradients[indexPath.row];
    return cell;
}

@end




@implementation ViewControllerCell {
    CALayer *_gradientLayer;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _gradientLayer.frame = self.contentView.bounds;
}

- (void)setIndex:(NSInteger)index {
    if (_index != index) {
        _index = index;
        if (_gradientLayer) { [_gradientLayer removeFromSuperlayer]; }
        _gradientLayer = JCUIGradientMake(index);
//        [self.contentView.layer addSublayer:_gradientLayer];
        [self.contentView.layer insertSublayer:_gradientLayer atIndex:0];
        [self setNeedsLayout];
    }
}

@end
